package com.zjl.hiring.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zjl.hiring.common.Constant;
import com.zjl.hiring.common.util.AppUtil;
import com.zjl.hiring.company.dao.ApplicationDao;
import com.zjl.hiring.company.dao.PositionDao;
import com.zjl.hiring.company.entity.ApplicationEntity;
import com.zjl.hiring.company.entity.PositionEntity;
import com.zjl.hiring.talent.dao.ResumeDao;
import com.zjl.hiring.talent.entity.ResumeEntity;

public class PositionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PositionDao positionDao = new PositionDao();
	private ResumeDao resumeDao = new ResumeDao();
	private ApplicationDao applicationDao = new ApplicationDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 解决乱码问题
		request.setCharacterEncoding("UTF-8");
		// 接受参数
		String command = request.getParameter("command");
		if ("listAllPosition".equals(command)) {
			listAllPosition(request, response);
		} else if ("detail".equals(command)) {
			detail(request, response);
		} else if ("apply".equals(command)) {
			apply(request, response);
		} else {
			throw new RuntimeException("请求失败！");
		}
	}

	private void listAllPosition(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 調用業務邏輯
		List<PositionEntity> positionList = positionDao.listAllPosition();
		// 转向
		request.setAttribute("positionList", positionList);
		request.getRequestDispatcher("view/company/position_list.jsp").forward(
				request, response);
		;
	}

	/**
	 * 查询职位的全部信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void detail(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		Object obj = request.getSession().getAttribute("talent");
		// 获取参数
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		// 调用业务逻辑
		PositionEntity position = positionDao.get(positionId);
		// 转向
		if (position == null) {
			response.sendRedirect("PositionServlet?command=listAllPosition&result="
					+ Constant.OPERATE_ERROR);
		} else {
			if (AppUtil.isLogin(request)) {
				// 查询所有职位
				int talentId = AppUtil.getTalentId(request, response);
				List<ResumeEntity> resumeList = resumeDao.list(talentId);
				request.setAttribute("resumeList", resumeList);
				// 查询职位状态
				ApplicationEntity application = applicationDao.applyed(positionId, talentId);
				request.setAttribute("application", application);
			}
				request.setAttribute("position", position);
				request.getRequestDispatcher("view/company/position-detail.jsp")
						.forward(request, response);
		}
	}

	/**
	 * 申请职位
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void apply(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		// 接受参数
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		int resumeId = Integer.parseInt(request.getParameter("resumeId"));
		// 调用业务逻辑
		int talentId = AppUtil.getTalentId(request, response);
		int row = applicationDao.add(positionId, talentId, resumeId);
		// 转向
		if (row == 1) {
			response.sendRedirect("PositionServlet?command=detail&positionId="
					+ positionId + "&result=" + Constant.OPERATE_SUCCESS);
		} else {
			response.sendRedirect("PositionServlet?command=detail&positionId="
					+ positionId + "&result=" + Constant.OPERATE_ERROR);
		}
	}
}
